package com.hnust.controller.personInfo;

import com.hnust.controller.paper.component.CheckCourseGoalController;
import com.hnust.domain.GroupInfo;
import com.hnust.domain.GroupMember;
import com.hnust.fx.FXMLController;
import com.hnust.service.TeacherServices;
import com.jfoenix.controls.JFXButton;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.util.StringConverter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.ResourceBundle;

@FXMLController
public class MyCourseGroupController implements Initializable {

    public HBox formContainer;
    public AnchorPane myCourseGroupWindow;
    public Label courseGroupLabel;
    public JFXButton showGoalBtn;
    public JFXButton searchBtn;
    @Autowired
    private TeacherServices teacherServices;
    @Autowired
    CheckCourseGoalController checkCourseGoalController;

    @FXML
    private Pagination page;
    @FXML
    private Label dataCount;
    @FXML
    private Label dataRows;
    @FXML
    private Label dataPages;
    @FXML
    TableView<GroupMember> tableSize;
    private int currentPage;
    @FXML
    ComboBox<GroupInfo> courseGroup;
    private String selectCourseGroupId;
    private String selectCourseId;
    @FXML
    Label name; //组长姓名
    @FXML
    Label leader_number; //组长教工号
    @FXML
    TextField input;

    private static final Logger logger = LogManager.getLogger(MyCourseGroupController.class);
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        input.prefWidthProperty().bind(formContainer.widthProperty().multiply(0.2));
        courseGroup.prefWidthProperty().bind(formContainer.widthProperty().multiply(0.2));
        //初始化
        currentPage=0;
        //数据转换
        myCourseGroupWindow.parentProperty().addListener((observable, oldValue, newValue) -> {
            if(newValue!=null){
                getCourseGroupInfo();
            }else{
                tableSize.setItems(null);
                selectCourseGroupId=null;
            }
        });
        courseGroup.setConverter(new StringConverter<GroupInfo>() {
            @Override
            public String toString(GroupInfo object) {
                return object.getName();
            }
            @Override
            public GroupInfo fromString(String string) {
                return null;
            }
        });
        courseGroup.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if(newValue!=null){
                logger.info("选中的是:"+newValue.getName());
                selectCourseGroupId = newValue.getId();
                selectCourseId = newValue.getCourseId();
                teacherServices.getCourseGroupDetailInfo(selectCourseGroupId,
                        leader_number,name,tableSize,currentPage);
            }
        });
        page.currentPageIndexProperty().addListener((observable, oldValue, newValue) -> {
            currentPage=newValue.intValue();
            teacherServices.getCourseGroupDetailInfo(selectCourseGroupId,leader_number,name,tableSize,currentPage);
        });
    }

    private void getCourseGroupInfo() {
        teacherServices.getCourseGroupInfo(courseGroup,courseGroupLabel);
    }

    //按姓名查找课程组成员
    public void search() {
        teacherServices.searchCourseGroupMember(searchBtn,tableSize,selectCourseGroupId,input.getText());
    }

    //重置输入框
    public void reset() {
        input.clear();
    }

    public void showGoal() {
        teacherServices.getCourseTargetList(showGoalBtn,selectCourseId);
    }

    public void setPage(int len){
        int a=len/10;
        if(len%10!=0){
            a++;
        }
        page.setPageCount(a);
        dataPages.setText("共"+a+"页");
        dataRows.setText("每页10条");
        dataCount.setText("共有"+len+"条数据");
    }
}






